<?php
/**
 * Template Name: Daftar Cerita
 *
 * @GramediaWritingProject
 * @version 1.0
 * @Epifana & Vincentia
 */
if (is_user_logged_in()){

get_header(); 

$thumbid= isset( $_GET['thumbid'] ) ? intval( $_GET['thumbid'] ) : 0;
  $bookid= isset( $_GET['bookid'] ) ? intval( $_GET['bookid'] ) : 0;
  $thumburl= $_GET['thumburl'] ;
 if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ))
  {
 
   $id = intval( $_POST['action']);
   $thumbidold = get_post_thumbnail_id($id);
 //  if(isset($_FILES['file'])&&(!empty($_FILES['file']))){
         foreach ($_FILES as $file => $array) {
	            wp_delete_attachment($thumbidold);//delete old thumbnail old
					 $newupload = insert_attachment($file,$bookid);
					 if ($newupload==0){ $error="Gambar gagal di unggah! Pastikan ukuran dan besar gambar sudah tepat.";}
	              	else {
					update_post_meta( $id, '_thumbnail_id', $newupload);
					}
	            }
		}

			
	     
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['hapus'] ))
  {
 
   $babid = intval( $_POST['hapus']);
    $boid = intval( $_POST['hapus2']);
   wp_delete_post($babid,true);
  delete_post_meta($boid, 'Chapter', $babid);
	
	    }
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['hapusbukuok'] ))
  {
  
    $boid = intval( $_POST['hapusbookid']);
  	$mykey = get_post_custom_values( 'Chapter' );
	if (count($mykey)>0){
		foreach ( $mykey as $key => $babid ) 
			{
			 wp_delete_post($babid,true); //delete all chapter
			}
	}
	wp_delete_post($boid,true); //delete book
	
}
?>
	<div class="row clearfix">
    <div class="col-md-12 column content">

		 <div class="row clearfix">
		<?php get_template_part( 'kolompertama2' ); ?>
	
        
		<div class="col-md-9 column" > <!-- untuk kolom kedua yang dibagi 2 -->
		<div class="row clearfix">
		 <div class="col-md-12 column">
                <div class="judulceritaterbaru2">Daftar Cerita </div>
                           <!-- start daftar cerita -->
						<?php echo "<p style='color:red'>".$error . "</p>";?>
						<?php get_template_part('authorbook');?>
         <br/>
                  
		 </div> <!--END class="col-md-12 column"-->
       </div><!--END class="row clearfix"-->
       </div> <!--END class="col-md-9 column"     --> 
	   </div>   <!--END class="row clearfix"-->	
	    </div>
       </div> <!--END class="col-md-12 column content"-->
	

	<!-- end untuk content -->
   
    
    <!-- untuk footer -->
	<?php

get_footer();
}
else { wp_redirect(get_site_url()); }
?>